/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan;

import java.util.ArrayList;
import java.util.List;

import com.cenluan.blog.Blog;
import com.cenluan.photo.Photo;
import com.cenluan.tweet.Tweet;
import com.cenluan.user.User;
import com.cenluan.vote.Vote;

public class IndexService {
	/**
	 * 前十热闹笔记
	 * 
	 * @return
	 */
	static List<Blog> hotBlogTop10() {
		StringBuilder sql = new StringBuilder();
		List<Object> paras = new ArrayList<Object>();
		sql.append(" from t_blog b ");
		sql.append(" left join ( ");
		sql.append(" select count(*) ct,blogId ");
		sql.append(" from t_blog_favorite ");
		sql.append(" group by blogId ");
		sql.append(" ) s on s.blogId=b.id ");
		sql.append(" where b.state=? ");
		paras.add(Blog.STATE_ACTIVE);
		sql.append(" order by s.ct desc ");
		return Blog.dao.paginate(1, 10, "select b.*", sql.toString(),
				paras.toArray()).getList();
	}

	/**
	 * 热门图片前十
	 * 
	 * @return
	 */
	static List<Photo> hotPhotoTop12() {
		StringBuilder sql = new StringBuilder();
		sql.append(" from t_photo p ");
		sql.append(" left join ( ");
		sql.append(" select count(*) ct,photoId ");
		sql.append(" from t_photo_favorite group by photoId ");
		sql.append(" ) pf on pf.photoId=p.id ");
		sql.append(" order by pf.ct desc,p.createTime desc ");
		return Photo.dao.paginate(1, 12, "select p.*", sql.toString())
				.getList();
	}

	/**
	 * 最新照片
	 * 
	 * @return
	 */
	static List<Photo> newPhotoTop12() {
		StringBuilder sql = new StringBuilder();
		sql.append(" from t_photo p ");
		sql.append(" order by p.createTime desc ");
		return Photo.dao.paginate(1, 12, "select p.*", sql.toString())
				.getList();
	}

	/**
	 * 活跃会员前10
	 * 
	 * @return
	 */
	static List<User> activeUserTop8() {
		StringBuilder sql = new StringBuilder();
		sql.append("from t_user ");
		sql.append(" where active=1 ");
		sql.append(" order by loginTime desc ");
		return User.dao.paginate(1, 8, "select *", sql.toString()).getList();
	}

	/**
	 * 热门微博
	 * 
	 * @return
	 */
	static List<Tweet> hotTweetTop10() {
		StringBuilder sql = new StringBuilder();
		sql.append(" from t_tweet t ");
		sql.append(" left join ( ");
		sql.append(" select count(*) ct,tweetId ");
		sql.append(" from t_tweet_comment ");
		sql.append(" group by tweetId ");
		sql.append(" ) s on s.tweetId=t.id ");
		sql.append(" order by s.ct desc,t.createTime desc ");
		return Tweet.dao.paginate(1, 10, "select t.* ", sql.toString())
				.getList();
	}

	/**
	 * 热门投票
	 * 
	 * @return
	 */
	static List<Vote> hotVoteTop10() {
		StringBuilder sql = new StringBuilder();
		List<Object> paras = new ArrayList<Object>();
		sql.append(" from t_vote ");
		sql.append(" where state=? ");
		paras.add(Vote.STATE_ACTIVE);
		sql.append(" order by totalUsers desc,createTime desc ");
		return Vote.dao.paginate(1, 10, "select *", sql.toString(),
				paras.toArray()).getList();
	}

}
